
//全局变量
var idea;
var typeNum;
var userIdId;
$(function(){

	// //判断设备 不同方法获取司机、乘客类别
	if(isIOS()){
		var dataNum1 = {
			"cancle":"0"
		}
		window.webkit.messageHandlers.test1.postMessage(dataNum1);	
		stringCount();	
		$(".feedback_btn").on("click",function(){
			content(userIdId,typeNum);		
		})
	}
	else{
		typeNum  = JavascriptInterface.getClientType();
		userIdId = JavascriptInterface.getUserId();
		stringCount();
		$(".feedback_btn").on("click",function(){
			content(userIdId,typeNum);
		})
	}	
})

//从客户端获取相关值之后进行Js操作
function content(userIdId,typeNum) {
	idea = $(".textAreaContent").val();
	$.ajax({
          type: "post",
          url:"http://pdhz-dddc.cn/dingding-web/feedbackAction!userFeedback",
          dataType: "jsonp",
          async: false,
          jsonp: "callback",
          jsonpCallback: "success_jsonp",
          data:{
             userId:userIdId,
             fbSource:typeNum,
             fbContent:idea
          },
          success: function(data){
              if(data.responseBody=="success"){
              	window.location.href="feedbackSuccess.html";
              }else if(data.responseBody=="error"){
              	swal({
		                title:"意见反馈失败,请重新提交!",
		                type: "warning",
		                confirmButtonColor: "#DD6B55",
		                confirmButtonText: "确定",
		            });
              }
          }
	});		
}

//限制textarea字符并提示剩余字符数、监听内容改变btn 封装函数方法
function textCounter(field, countfield, maxlimit) {  
   // 函数，3个参数，表单名字，表单域元素名，限制字符；  
   if( field.value.length !==0 && /\s*\S+/.test($(".textAreaContent").val())||field.value.length < maxlimit&& /\s*\S+/.test($(".textAreaContent").val())){
	   $(".feedback_btn").css({"background":"#FF4C4B","color":"#FFFFFF"});   
	   $(".feedback_btn").prop("disabled",false);
   }
   if(field.value.length == 0){
   	   //全部为空格或无字符 禁用btn 致灰
	   $(".feedback_btn").css("background","gray");
	   $(".feedback_btn").prop("disabled",true);
   } 
}  

//IOS回调函数
function getUserId(userId){	
	userIdId = userId;	
}
function getUserType(type){	
	typeNum = type;		
}

//textarea文本域监听封装函数
function stringCount(){
	$.event.special.valuechange = {

    teardown: function (namespaces) {
      $(this).unbind('.valuechange');
    },

    handler: function (e) {
      $.event.special.valuechange.triggerChanged($(this));
    },

    add: function (obj) {
      $(this).on('keyup.valuechange cut.valuechange paste.valuechange input.valuechange', obj.selector, $.event.special.valuechange.handler)
    },

    triggerChanged: function (element) {
      var current = element[0].contentEditable === 'true' ? element.html() : element.val()
        , previous = typeof element.data('previous') === 'undefined' ? element[0].defaultValue : element.data('previous')
      if (current !== previous) {
          element.trigger('valuechange', [element.data('previous')])
          element.data('previous', current)
        }
      }
  }
  $('textarea').on('valuechange', function (e, previous) {
     if($('.textAreaContent').val().length<500){
        $(".charsNum").val(500 - $('.textAreaContent').val().length); 
     }
     if($('.textAreaContent').val().length>500){
        $(".charsNum").val(0); 
        $('.textAreaContent').val($('.textAreaContent').val().substring(0,500));            
     }   
  })
}